package com.copm.ifm.servers.sys.controller.sys;

import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.api.pojo.PageVO;
import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.dto.SysTokenDTO;
import com.copm.ifm.servers.sys.entity.SysToken;
import com.copm.ifm.servers.sys.service.process.SysTokenService;
import com.copm.ifm.servers.sys.vo.SysTokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * token表相关接口类
 *
 * @author tp
 * @date 2021/1/21 11:39
 */
@Slf4j
@Api(tags = "token管理/sys-token")
@RestController
@RequestMapping("/sys-token")
public class SysTokenController extends BaseController {

    @Autowired
    SysTokenService sysTokenService;

    @ApiOperation("分页")
    @PostMapping("/page")
    public ServiceResponse<PageVO<SysTokenVO>> page(@RequestBody PageDTO<SysTokenDTO> param) {
        PageVO<SysTokenVO> result = sysTokenService.selectForPage(param);
        return success(result);
    }

    @ApiOperation("查询by id")
    @GetMapping("/by/id")
    public ServiceResponse<SysTokenVO> getById(@RequestParam Long id) {
        return success(sysTokenService.selectVoById(id));
    }

    @ApiOperation("保存")
    @PostMapping
    public ServiceResponse<?> save(@RequestBody SysTokenDTO addDTO) {
        sysTokenService.add(addDTO);
        return success();
    }

    @ApiOperation("更新")
    @PutMapping
    public ServiceResponse<SysToken> update(@RequestBody SysToken sysToken) {
        sysTokenService.updateEntityById(sysToken);
        return success(sysToken);
    }

    @ApiOperation("删除-根据by id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam Long id) {
        SysToken sysToken = sysTokenService.selectById(id);
        if (StringUtils.isEmpty(sysToken)) {
            return fail("删除失败，无效数据");
        }
        sysTokenService.updateDeleted(id);
        return success();
    }
}
